package com.lichuang.dedao.city.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lichuang.dedao.city.entity.Note;
import com.lichuang.dedao.city.entity.NoteLike;
import com.lichuang.dedao.city.entity.User;
import com.lichuang.dedao.city.fingnclient.UserFeignClient;
import com.lichuang.dedao.city.mapper.NoteMapper;
import com.lichuang.dedao.city.service.INoteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.client.RestTemplate;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 * 笔记 服务实现类
 * </p>
 *
 * @author wp
 * @since 2019-07-13
 */
@Service
public class NoteServiceImpl extends ServiceImpl<NoteMapper, Note> implements INoteService {


    @Autowired
    private UserFeignClient userFeignClient;

    public List<Note> selAllNote(@CookieValue("loginToken") String loginToken) {
        User user = userFeignClient.getCurrentUser(loginToken);
        List<User> list = userFeignClient.selectFollowedUser(user.getUserId());
        list.add(user);
        Note note = new Note();
        List<List<Note>> list1 = new LinkedList<>();
        for (User user1 : list) {
            note.setUserId(user1.getUserId());
            note.setUserimg(user1.getProfilePhoto());
            note.setUsername(user1.getNickName());
            QueryWrapper queryWrapper = new QueryWrapper(note);
            list1.add(baseMapper.selectList(queryWrapper));
        }
        List<Note> notes = new LinkedList<>();
        for (List<Note> noteList : list1) {
            for (Note note1 : noteList) {
                for (Note note2 : noteList){
                    if(note2.getTranspondId()==note1.getNoteId()){
                        note1.getNextNodes().add(note2);
                    }
                }
                for (User user1:list){
                    if(user1.getUserId()==note1.getUserId()){
                        note1.setUsername(user1.getNickName());
                        note1.setUserimg(user1.getProfilePhoto());
                    }
                }
                long minutes = Duration.between(note1.getShareTime(), LocalDateTime.now()).toMinutes();
                note1.setLeadtime(minutes + "分钟前");
                if(minutes>60){
                    minutes = Duration.between(note1.getShareTime(),LocalDateTime.now()).toHours();
                    note1.setLeadtime(minutes + "小时前");
                    if(minutes>24){
                        minutes = Duration.between(note1.getShareTime(),LocalDateTime.now()).toDays();
                        note1.setLeadtime(minutes + "天前");
                        if(minutes>30){
                            minutes = Duration.between(note1.getShareTime(),LocalDateTime.now()).toDays()/30;
                            note1.setLeadtime(minutes + "月前");
                            if(minutes>12){
                                minutes = Duration.between(note1.getShareTime(),LocalDateTime.now()).toDays()/30;
                                long yer = 0;
                                while (minutes>12){
                                    minutes-=12;
                                    yer+=1;
                                }
                                note1.setLeadtime(yer + "年前");
                            }
                        }
                    }
                }
                notes.add(note1);
            }
        }
        Collections.sort(notes, new Comparator<Note>() {
            @Override
            public int compare(Note o1, Note o2) {
                if (o1.getShareTime().isBefore(o2.getShareTime())) {
                    return 1;
                }
                if (o1.getShareTime().isAfter(o2.getShareTime())){
                    return -1;
                }
                return 0;
            }
        });
        return notes;
    }


    @Autowired
    NoteMapper noteMapper;


    /**
     * 插入笔记
     * @param note
     * @return
     */
    public int instranspond(Note note){
        return noteMapper.instranspond(note);
    }


    /**
     * 更新点赞数
     * @param note_id
     * @param
     * @return
     */
    public int updlike(Integer uid,Integer note_id){

        int likes = 0;
        NoteLike noteLike = noteMapper.selLike(note_id,uid);
        if(noteLike == null){
            noteLike.setNoteId(note_id);
            noteLike.setUserId(uid);
            noteLike.setState(1);
            int ins = noteMapper.insLike(noteLike);
            likes+=1;
        }else if (noteLike.getState() == 0){
            int  state = noteMapper.updlike(noteLike.getId(),1);
            likes+=1;
        }else if(noteLike.getState()==1){
            int state = noteMapper.updlike(noteLike.getId(),0);
            likes-=1;
        }

        return noteMapper.updLike(uid,note_id,likes);
    }

//    public List<Note> selAll(){
////        List<User> list = restTemplate.getForEntity("http/user/selectFollowedUser",List.class)
////        return ;
//    }



}
